<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>Document</title>
  <style>
    #main {
      border:1px solid black;
    }
  </style>
</head>
<body>

   
   <canvas id="main" width="500" height="500"></canvas>

  <script>

    const arr = [
      {
        x: 20,
        y: 100
      },
      {
        x: 100,
        y: 200
      },
      {
        x: 30,
        y: 300
      }
    ]
    var draw = document.getElementById('main');
    var ctx = draw.getContext('2d');

    function initCtx() {
        // 清除画布
        // ctx.clearRect(0, 0, 500, 500);
        // ctx.strokeStyle = '#333';
        // ctx.lineWidth = 1;
        // 通过for, 循环执行画线动作
      for(let i = 0; i < arr.length; i++) {
        console.log(i)
        ctx.beginPath();
        ctx.moveTo(arr[i].x, arr[i].y);
        ctx.lineTo(arr[i+1].y, arr[i+1].y);
        ctx.stroke();
      }
    }
    initCtx()

  </script>
</body>
</html>